package com.zim.ta_service.base.sequence;

import com.zim.ta_service.base.exception.BizMsgException;
import com.zim.ta_service.base.sequence.enums.RedisSeqBizType;
import com.zim.ta_service.base.sequence.enums.SeqType;
import com.zim.ta_service.base.sequence.enums.UniqueKeyBizType;
import com.zim.ta_service.base.sequence.generator.*;
import lombok.Builder;

/**
 * @author alabimofa
 */
@Builder
public class SeqBuilder {

    private final SeqType seqType;

    private final RedisSeqBizType redisSeqBizType;

    private final UniqueKeyBizType uniqueKeyBizType;

    public Long getNumberId() {
        IGenerator generator = getGenerator();
        return generator.nextId();
    }

    public String getStringId() {
        return getGenerator().nextIdString();
    }

	public static SeqBuilder redisSeq(RedisSeqBizType redisSeqBizType) {
		return SeqBuilder.builder()
			.seqType(SeqType.REDIS)
			.redisSeqBizType(redisSeqBizType)
			.build();
	}

	public static SeqBuilder uuidSeq() {
		return SeqBuilder.builder()
			.seqType(SeqType.UUID)
			.build();
	}

	public static SeqBuilder snowFlakeSeq() {
		return SeqBuilder.builder()
			.seqType(SeqType.SNOWFLAKE)
			.build();
	}

    private IGenerator getGenerator() {
        IGenerator generator;
        switch (seqType) {
            case UUID:
                generator = new UuidGenerator();
                break;
            case REDIS:
                generator = new RedisGenerator(redisSeqBizType);
                break;
            case SNOWFLAKE:
                generator = new SnowFlakeGenerator();
                break;
            case UNIQUE_KEY:
                generator = new UniqueKeyGenerator(uniqueKeyBizType);
                break;
            default:
                throw new BizMsgException("不支持的序列号生成类型:".concat(seqType.name()));
        }
        return generator;
    }
}
